package com.vhly.ddxdecompiler.dexreader.util;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-2-18
 * Time: 19:56:46
 */
public final class Leb128Utils {

    private Leb128Utils() {
    }

    public static int unsignedLeb128Size(int value) {
        int remaining = value >> 7;
        int count;
        for (count = 0; remaining != 0; count++) {
            value = remaining;
            remaining >>= 7;
        }

        return count + 1;
    }

    public static int signedLeb128Size(int value) {
        int remaining = value >> 7;
        int count = 0;
        boolean hasMore = true;
        int end = (value & 0x80000000) != 0 ? -1 : 0;
        while (hasMore) {
            hasMore = remaining != end || (remaining & 1) != (value >> 6 & 1);
            value = remaining;
            remaining >>= 7;
            count++;
        }
        return count;
    }
}